"use strict";

var router = require("router"),
    TokenService = require("router").TokenService,
    mongo = require('../core/mongoservice'),
    Q = require('q');

function RouteService() {
    console.log("RouteService constructor");
}

RouteService.prototype = Object.create(TokenService.prototype);
RouteService.prototype.constructor = RouteService;

RouteService.prototype.connect = function (request, response, promise) {
    return promise.then(function (self) {
        var qshopId = request.query.shopId;

        return require("../userservice").mongoGetByLoginAndPassword(request.body.login, request.body.password).then(function (user) {
            if (!user) throw new Error("User is not defined.");
            return require("../userservice").mongoGetEmployers(user._id).then(function (shops) {
                var shopId = null;
                if (shops.length == 0) throw new Error(user.login + " .");
                else if (shops.length == 1) shopId = shops[0]._id.toString();
                else {
                    var res = shops.filter(function (e) {
                        return e._id.toString() == qshopId;
                    });
                    if (res.length == 0)
                        return response.send({ request: request, statusCode: 500, content: { shops: shops} });

                    shopId = res[0]._id.toString();
                };

                var payload = { user: { id: user._id.toString() }, shop: { id: shopId} }
                var token = self.encode(payload);
                return response.send({ request: request, content: { route: "/pro", token: token} });
            });
        });
    });
};

RouteService.prototype.connectFromToken = function (request, response, promise) {
    return promise.then(function (self) {
        self.identify(request, response);

        var userId = request.payload.user.id;
        var qshopId = request.query.shopId;

        return require("../userservice").mongoGetById(userId).then(function (user) {
            if (!user) throw new Error("User is not defined.");
            return require("../userservice").mongoGetEmployers(user._id).then(function (shops) {
                var shopId = null;
                if (shops.length == 0) throw new Error(user.login + " .");
                else if (shops.length == 1) shopId = shops[0]._id.toString();
                else {
                    var res = shops.filter(function (e) {
                        return e._id.toString() == qshopId;
                    });
                    if (!res.length)
                        return response.send({ request: request, statusCode: 500, content: { shops: shops} });

                    shopId = res[0]._id.toString();
                };

                var payload = { user: { id: user._id.toString() }, shop: { id: shopId} }
                var token = self.encode(payload);
                return response.send({ request: request, content: { route: "offers", token: token} });
            });
        });
    })
};

RouteService.prototype.index = function (request, response, promise) {
    return promise.then(function (self) {
        request.url = "/pro/main.html";
        return router.assets.invoke(request, response);
    });
};

exports = module.exports = new RouteService();